var bingAPI = require('./BingAPI');
var bingProperties = require('./bingProperties');
var ET = bingProperties.ET;
var $ = require('jquery');
var fs = require('fs');
//soap.send (option, soapStr, callback);
// var userName =  'AlibaPI';
var SOAP = bingProperties.SOAP;
var STATIC = bingProperties.STATIC;
var REPORTTYPE = bingProperties.REPORTTYPE;

var customerAccountId = STATIC.customerAccountId;
var action = {
    GetCampaignsByAccountId: 'GetCampaignsByAccountId',
    SubmitGenerateReport: 'SubmitGenerateReport',
    PollGenerateReport: 'PollGenerateReport'
};
var developerToken = STATIC.developerToken;
var password = STATIC.password;
var userName = STATIC.userName;

function GetCampaignCallback(err, data) {
    if(!err) {
        var msg = data.toString();
        // console.log(11111111111111111, msg);
        var xmlTree = ET.parse(msg);
        var campaignList = xmlTree.findall('.//Campaign');
        $.each(campaignList, function(idx, campaign) {
            console.log(
            // 'BudgetType',campaign.find('.//BudgetType').text,'\t',
            // 'ConversionTrackingEnabled',campaign.find('.//ConversionTrackingEnabled').text,'\t',
            // 'DaylightSaving',campaign.find('.//DaylightSaving').text,'\t',
            // 'Description',campaign.find('.//Description').text,'\t',
            'Id', campaign.find('.//Id').text, '\t',
            // 'MonthlyBudget',campaign.find('.//MonthlyBudget').text,'\t',
            'Name', campaign.find('.//Name').text, '\t'
            // 'Status',campaign.find('.//Status').text//,\t,
            // 'TimeZone',campaign.find('.//TimeZone').text\t,
            );

        })
        // console.log(msg);
        // console.log(msg)
        // console.log('.//{ '+SOAP['ns_bingads']+' }TrackingId')
        // console.log(xmlTree.findtext('.//{ '+SOAP['ns_bingads']+' }TrackingId'));
    } else {
        console.error('error ', err.xml.toString());
        // console.log('fault', xmlTree.findtext('.//faultcode', './/faultstring'))
    }

}


var ReportRequest = require('./report').ReportRequest;
var report = new ReportRequest();
report.setCustomerAccountId(1430357);
report.setDeveloperToken('OM7HS4S5');
report.setPassword('brysjhhrh6');
report.setUserName('Alibaba_API');
report.setAction('SubmitGenerateReport');

var kRequest = report.createKeywordPerformanceReportRequest();
var format = report.createReportFormat('Xml');
var language = report.createReportLanguage('English');
var reportName = report.createReportName('My test report');
var returnOnlyCompleteData = report.createReturnOnlyCompleteData(false);

// kRequest.setFormat(format.target);
// kRequest.setLanguage(language.target);
kRequest.setReportName(reportName.target);
// kRequest.setReturnOnlyCompleteData(returnOnlyCompleteData.target);
kRequest.setAggregation('Daily');
var columns = report.createColumns();
columns.addColumn('TimePeriod');
columns.addColumn('AccountName');
columns.addColumn('AverageCpc');
kRequest.setColumns(columns.target);
// var filter = report.createFilter();
var accountIds = report.createAccountIds();
accountIds.addAccountId(1430357);
var scope = report.createScope();
scope.setAccountIds(accountIds.target);
kRequest.setScope(scope.target);
var time = report.createTime();
time.setPredefinedTime('LastSevenDays');

kRequest.setTime(time.target);


function reportCallback(err, data) {
    if(!err) {
        // var rst = 

        //return {status:'',downloadURL:''}
        // console.log('reportCallback', reportRequestId);
    } else {

    }

}

report.on('reportDataReady', function(filePath) {
    console.log(111111111111111111111, filePath);
})


// console.log(
//     report.toXMLString()
// );
// bingAPI.campaignManagement.campaignManagementService(action.GetCampaignsByAccountId, customerAccountId, developerToken, password, userName, GetCampaignCallback);
// bingAPI.reporting.reportingService(action.SubmitGenerateReport, customerAccountId, developerToken, password, userName,REPORTTYPE.KeywordPerformanceReportRequest, reportCallback);
bingAPI.reporting.reportingService(action.SubmitGenerateReport, report, reportCallback);